package com.orhon.smartcampus.basicComponents.service;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.orhon.smartcampus.modules.base.dao.DictionaryDao;
import com.orhon.smartcampus.modules.base.dao.RegionsDao;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Regions;


@Service
public class GQLRunService implements ApplicationRunner {
	@Autowired
	private DictionaryDao dictionaryDao;
	@Autowired
	private RegionsDao regionsDao;


	private static HashMap<String, Object> DictionaryMap  = new HashMap<String, Object>();
	
	private static HashMap<String, String> DictionaryNameMap  = new HashMap<String, String>();

	private static HashMap<String, Object> RegionMap  = new HashMap<String, Object>();



	@Override
	public void run(ApplicationArguments args) throws Exception {		
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();  
		service.scheduleAtFixedRate(runnable, 1, 60*10, TimeUnit.SECONDS);  
	}

	Runnable runnable = new Runnable() {  
		public void run() {  
			String hql = "from Dictionary";
			List<Dictionary>  list = dictionaryDao.find(hql,null,0,0);
			for (Dictionary dictionary : list) {
				DictionaryMap.put(dictionary.getDictionaryValue(), dictionary);
				
				JSONObject parse = JSONObject.parseObject(dictionary.getDictionaryName());
				String zhName = (String) parse.get("zh");
				DictionaryNameMap.put(zhName, dictionary.getDictionaryValue());
			}
			String hql1 = "from Regions ";
			List<Regions> list1 = regionsDao.find(hql1,null,0,0);
			for (Regions regions : list1) {
				if(regions.getId()!=null && regions.getRegionName()!=null) {
					RegionMap.put(regions.getId().toString(),regions);
				}
			}
		}  
	};  
	public static HashMap<String, String> DictionaryNameMap() {
		return DictionaryNameMap;
	}

	public static HashMap<String, Object> DictionaryMap() {
		return DictionaryMap;
	}

	public static HashMap<String, Object> RegionMap() {
		return RegionMap;
	}



}
